package com.iv.durian.cloud.api;

import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.StudentLession;
import com.iv.durian.cloud.api.service.StudentLessonService;
import com.iv.durian.cloud.vo.StudentLessionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/StudentLesson")
@Slf4j
@Api(value = "StudentLessonApi" ,description = "StudentLessonApi",tags={"StudentLession"} )
public class StudentLessionApi {

    @Autowired
    private StudentLessonService studentLessonService;

    @ApiOperation(value = "新建学员课程")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(
            @ApiParam(name = "lessionId", value = "课程Id", required = true) @RequestParam(value = "lessionId") Integer lessionId,
            @ApiParam(name = "studentId", value = "学员Id", required = true) @RequestParam(value = "studentId") Integer studentId
    ) {
        StudentLession studentLession = new StudentLession();
        studentLession.setLessionId(lessionId);
        studentLession.setStudentId(studentId);
        studentLessonService.add(studentLession);
    }

    @ApiOperation(value = "删除学员课程")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public void remove(
            @ApiParam(name = "id",value = "学员课程Id") @RequestParam(value = "id") Integer id
    ){
        log.debug("删除的学院课程Id:" +id);
        studentLessonService.remove(id);
    }

    @ApiOperation(value = "修改学员课程状态")
    @RequestMapping(value = "/updateStatust", method = RequestMethod.POST)
    public void updateStatust(
            @ApiParam(name = "studentLessionId",value = "学员课程Id") @RequestParam(value = "studentLessionId") Integer studentLessionId,
            @ApiParam(name = "statust",value = "学员课程状态 0-NONE 1-有效") @RequestParam(value = "statust") Integer statust
    ){
        StudentLession studentLession =new StudentLession();
        studentLession.setId(studentLessionId);
        studentLession.setStatust(statust);
        log.debug("学员课程状态修改：" +studentLessionId);
        studentLessonService.updateStatust(studentLession);
    }

    @ApiOperation(value = "修改学员课程")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(
            @ApiParam(name = "studentLessionId",value = "学员课程Id") @RequestParam(value = "studentLessionId") Integer studentLessionId,
            @ApiParam(name = "lessionId",value = "课程Id") @RequestParam(value = "lessionId") Integer lessionId
    ){
        StudentLession studentLession = new StudentLession();
        studentLession.setId(studentLessionId);
        studentLession.setLessionId(lessionId);
        log.debug("学员课程修改：" +studentLessionId);
        studentLessonService.update(studentLession);
    }

    @ApiOperation(value = "查询学员课程")
    @RequestMapping(value = "/get/{studentLessionId}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<StudentLession>> get(
            @ApiParam(required = true, name = "studentLessionId", value = "学员课程Id") @RequestParam("studentLessionId") Integer studentLessionId
    ) {
        JsonResult r = new JsonResult();

        try {
            StudentLession studentLession = studentLessonService.get(studentLessionId);
            if (studentLession != null) {
                StudentLessionVo studentLessionVO = StudentLessionVo.builder()
                        .id(studentLession.getId())
                        .studentName(studentLession.getStudentName())
                        .lessonName(studentLession.getLessonName())
                        .teacher(studentLession.getTeacher())
                        .lessionId(studentLession.getLessionId())
                        .studentId(studentLession.getStudentId())
                        .source(studentLession.getSource())
                        .statust(studentLession.getStatust())
                        .createTime(studentLession.getCreateTime())
                        .updateTime(studentLession.getUpdateTime()).build();
                r.ok(studentLessionVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("学员课程信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }
}
